package com.ruoyi.project.his.controller;

import com.ruoyi.common.constant.UserConstants;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.framework.aspectj.lang.annotation.Log;
import com.ruoyi.framework.aspectj.lang.enums.BusinessType;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.framework.web.page.TableDataInfo;
import com.ruoyi.project.his.domain.HisDept;
import com.ruoyi.project.his.service.IHisDeptService;
import com.ruoyi.project.system.domain.SysPost;
import net.bytebuddy.asm.Advice;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/his/dept")
public class HisDeptController extends BaseController {
    @Autowired
    private IHisDeptService hisDeptService;

    @PreAuthorize("@ss.hasPermi('his:dept:list')")
    @GetMapping("/list")
    public TableDataInfo list(HisDept hisDept){
        // 不调用这个方法，分页无效
        startPage();
        List<HisDept> list = hisDeptService.selectDeptList(hisDept);
        return getDataTable(list);
    }

    @PreAuthorize("@ss.hasPermi('his:dept:add')")
    @Log(title = "科室管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@Validated @RequestBody HisDept hisDept){
        hisDept.setCreateBy(SecurityUtils.getUsername());
        return toAjax(hisDeptService.insertDept(hisDept));
    }

    @PreAuthorize("@ss.hasPermi('his:dept:remove')")
    @Log(title = "科室管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/{deptIds}")
    public AjaxResult remove(@PathVariable Long[] deptIds)
    {
        return toAjax(hisDeptService.deleteDeptByIds(deptIds));
    }


    @Log(title = "科室管理", businessType = BusinessType.EXPORT)
//    @PreAuthorize("@ss.hasPermi('his:dept:export')")
    @GetMapping("/export")
    public AjaxResult export(HisDept hisDept)
    {
        List<HisDept> list = hisDeptService.selectDeptList(hisDept);
        ExcelUtil<HisDept> util = new ExcelUtil<HisDept>(HisDept.class);
        return util.exportExcel(list, "科室数据");
    }

    @PreAuthorize("@ss.hasPermi('his:dept:edit')")
    @Log(title = "科室管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@Validated @RequestBody HisDept dept)
    {
        dept.setUpdateBy(SecurityUtils.getUsername());
        return toAjax(hisDeptService.updateDept(dept));
    }

    @PreAuthorize("@ss.hasPermi('his:dept:query')")
    @GetMapping(value = "/{deptId}")
    public AjaxResult getInfo(@PathVariable Long deptId)
    {
        return AjaxResult.success(hisDeptService.selectDeptById(deptId));
    }
}
